package com.xiong.xtools.searchbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.view.MenuItem;
import android.widget.PopupMenu;
import android.widget.Toast;

import com.xiong.xtools.R;
import com.xiong.xtools.searchbox.SearchBox.MenuListener;
import com.xiong.xtools.searchbox.SearchBox.SearchListener;

import java.util.ArrayList;
/**
 * Author : 熊文强
 * Time : 2017/9/19
 * Content : 自定义动画搜索框
 */
public class SearchActivity extends AppCompatActivity {
	private SearchBox search;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);

		search = (SearchBox) findViewById(R.id.searchbox1);
		search.enableVoiceRecognition(this);
		for(int x = 0; x < 10; x++){
			SearchResult option = new SearchResult("Result " + Integer.toString(x), getResources().getDrawable(R.mipmap.search_history));
			search.addSearchable(option);
		}
		search.setMenuListener(new MenuListener(){

			@Override
			public void onMenuClick() {
				//Hamburger has been clicked
				Toast.makeText(SearchActivity.this, "Menu click", Toast.LENGTH_LONG).show();
			}

		});
		search.setSearchListener(new SearchListener(){

			@Override
			public void onSearchOpened() {
				//Use this to tint the screen
			}

			@Override
			public void onSearchClosed() {
				//Use this to un-tint the screen
			}

			@Override
			public void onSearchTermChanged(String term) {
				//React to the search term changing
				//Called after it has updated results
			}

			@Override
			public void onSearch(String searchTerm) {
				Toast.makeText(SearchActivity.this, searchTerm +" Searched", Toast.LENGTH_LONG).show();
			}

			@Override
			public void onResultClick(SearchResult result) {
				//React to a result being clicked
			}

			@Override
			public void onSearchCleared() {
				//Called when the clear button is clicked
			}

		});
		search.setOverflowMenu(R.menu.search_overflow_menu);
		search.setOverflowMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				switch (item.getItemId()) {
					case R.id.test_menu_item:
						Toast.makeText(SearchActivity.this, "Clicked!", Toast.LENGTH_SHORT).show();
						return true;
				}
				return false;
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1234 && resultCode == RESULT_OK) {
			ArrayList<String> matches = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			search.populateEditText(matches.get(0));
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
